package com.liukai.bishe.ui.dialog;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.liukai.bishe.R;

import androidx.appcompat.widget.AppCompatButton;

public class UserDefineTimeDialog {

    private Context mContext;
//    private final AlertDialog.Builder mBuilder;

    private int mTransactionTime = 1; // 事务时间
    private AlertDialog mDialog = null;
    private final View mView_dialog;
    private EditText mTransactionTime1;
    private AppCompatButton mBtnSure;
    private Handler mHandler;

    public UserDefineTimeDialog(Context context, String time){

        this.mContext = context;

        mView_dialog = View.inflate(context,R.layout.dialog_user_define_time,null);

        if (mDialog == null){
            // 创建AlertDialog对象
            mDialog = new AlertDialog.Builder(mContext).create();
            mDialog.show();
            // 设置点击可取消
            mDialog.setCancelable(true);
            // 获取Window对象
            Window window = mDialog.getWindow();
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
            // 设置显示视图内容
            window.setContentView(mView_dialog);

//            WindowManager.LayoutParams lp = mDialog.getWindow().getAttributes();
//            lp.width = 500;// 调整该值可以设置对话框显示的宽度
//            lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
//            window.setAttributes(lp);
        }else {
            mDialog.show();
        }

        initView(time);
        initListener();


    }

    private void initListener() {

        /**
         * 点击确定按钮，将设置的事务时间传回上一个dialog
         */
        mBtnSure.setOnClickListener(view -> {

            String time = mTransactionTime1.getText().toString();
            if (time != null && !time.equals("")){
                mTransactionTime = Integer.parseInt(time);
            }
            if(mHandler != null){
                Message message = Message.obtain();
                message.what = 1;
                message.obj = mTransactionTime;
                mHandler.sendMessage(message);
            }

            mDialog.dismiss();


        });

    }

    private void initView(String time) {

        mTransactionTime1 = mDialog.findViewById(R.id.transactionTime);
        mBtnSure = mDialog.findViewById(R.id.btnSure);

        if (time != null){
            mTransactionTime1.setText(time);
        }


    }

    public void show(){
        mDialog.setCanceledOnTouchOutside(false);
//        mDialog.show();
    }

    public void setHandler(Handler handler) {
        this.mHandler = handler;
    }
}
